#include <iostream>
#include <cmath>
typedef long long ll;
using namespace std;

ll gcd(ll a, ll b){
	if(b==0) return a;
	else return gcd(b,a%b);
}
ll lcm(ll a, ll b){
	return a*b/gcd(a,b);
}

void solve()
{
	ll a0=-1,a1=-1,b0=-1,b1=-1;
	cin>>a0>>a1>>b0>>b1;
	
	int ans=0;
	for(int i=1;i<=sqrt(b0);++i){
		if(b0%i==0){
			int x=b1/b0*i;
			if(lcm(x,b0)==b1 && gcd(x,a0)==a1) ++ans;
			x=b1/b0*(b0/i);
			if(b0/i!=i && lcm(x,b0)==b1 && gcd(x,a0)==a1) ++ans;
		}
	}
	
	cout<<ans<<endl;
}

int main()
{
	int n; cin>>n;
	while(n--) solve();
	
	return 0;
}



